#include <iostream>

using namespace std;

const int MAXV = 1000, MAXE = 10000, INF = 1000000005;
 
struct DfsFlow
{
    int V, source, sink, start, vis [MAXV];
    int eind, eadj [MAXE], ecap [MAXE], eprev [MAXE], elast [MAXV];
 
    inline void init (int v)
    {
        V = v; eind = start = 0;
        memset (elast, -1, V * sizeof (int));
    }
 
    inline void addedge (int a, int b, int cap1, int cap2)
    {
        eadj [eind] = b; ecap [eind] = cap1; eprev [eind] = elast [a]; elast [a] = eind++;
        eadj [eind] = a; ecap [eind] = cap2; eprev [eind] = elast [b]; elast [b] = eind++;
    }
 
    long long dfs (int num, long long pcap)
    {
        if (num == sink)
            return pcap;
 
        if (vis [num] == start)
            return 0;
 
        vis [num] = start;
        long long sum = 0;
 
        for (int i = elast [num]; i != -1; i = eprev [i])
            if (ecap [i]> 0)
            {
                long long p = dfs (eadj [i], min (pcap, (long long) ecap [i]));
                ecap [i] -= p;
                ecap [i ^ 1] += p;
                pcap -= p;
                sum += p;
 
                if (pcap == 0)
                    break;
            }
 
        return sum;
    }
 
    long long flow (int _source, int _sink)
    {
        if (V == 0)
            return -1;
 
        memset (vis, -1, sizeof (vis));
        source = _source; sink = _sink;
        long long total = 0, aug;
 
        do
        {
            aug = dfs (source, INF);
            total += aug;
            start++;
        }
        while (aug > 0);
 
        return total;
    }
};
 
DfsFlow graph;

int main( )
{
    int n, m, s, e, c, i;
    while ( scanf("%d%d", &n, &m) != EOF )
    {
        graph.init( n );
        for ( i = 0; i < n; i++ )
        {
            scanf("%d%d%d", &s, &e, &c);
            graph.addedge( s - 1, e - 1, c, 0 );
        }
        printf("%I64d\n", graph.flow( 0, m - 1 ));
    }
    return 0;
}
